package org.maitaole.controller;

import org.maitaole.entity.SysUser;
import org.maitaole.entity.TbCart;
import org.maitaole.model.Result;
import org.maitaole.service.ICartInfoService;
import org.maitaole.service.ISysUserService;
import org.maitaole.service.ITbCartDetailService;
import org.maitaole.service.ITbCartService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order")
@CrossOrigin("*")
public class OrderAndCartController {

    @Resource
    private ICartInfoService iCartInfoService;

    @Resource
    private ITbCartDetailService iTbCartDetailService;

    @Resource
    private ITbCartService iTbCartService;

    @Resource
    private ISysUserService iSysUserService;

    @GetMapping("/show")
    public Result getAllCartInfo( String username ){
        return Result.buildSuccess(iCartInfoService.getAllCartInfo(username));
    }

    @PostMapping("/addCart")
    @Transactional(propagation = Propagation.REQUIRED)
    public Result insertSelective(String fkCartUserName, Integer fkCartGoodId, Integer cartGoodPrice,  String stepTwoDetailId){
        System.out.println("fkCartUserName = " + fkCartUserName);
        System.out.println("fkCartGoodId = " + fkCartGoodId);
        System.out.println("cartGoodPrice = " + cartGoodPrice);
        System.out.println("stepTwoDetailId = " + stepTwoDetailId);
//        return Result.buildSuccess("success");
        int i = iTbCartService.insertTbCart(fkCartUserName, fkCartGoodId, cartGoodPrice);
        int i1 = iTbCartDetailService.insertTbCartDetails(stepTwoDetailId);
        if (i != 1 || i1 == 0){
            return Result.buildFail("fail");
        } else {
            return Result.buildSuccess("success");
        }
    }

}
